package com.example.chenzixin.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.chenzixin.adapter.ChengyuAdapter;
import com.example.chenzixin.viewmodel.ChengyuViewModel;
import com.example.read.R;
import com.hjq.shape.view.ShapeEditText;
import com.hjq.shape.view.ShapeTextView;

public class ChengYuActivity extends AppCompatActivity {
    private ChengyuViewModel viewModel = new ChengyuViewModel();

    private ChengyuAdapter adapter = new ChengyuAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengyu_search);

        AppCompatImageView iv_back = findViewById(R.id.iv_back);
        ShapeEditText et_search = findViewById(R.id.et_search);
        RecyclerView rv_list = findViewById(R.id.rv_list);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ShapeTextView tv_search = findViewById(R.id.tv_search);
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.search(et_search.getText().toString());
            }
        });
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        rv_list.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                Intent intent = new Intent(ChengYuActivity.this, ChengYuDetailsActivity.class);
                intent.putExtra("text", adapter.getItem(position).get("name"));
                startActivity(intent);
            }
        });

        viewModel.eventList.observe(this,  list -> adapter.setNewData(list));
    }
}